package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Menu;
import com.itheima.health.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <dd>描述: ~ MenuController</dd>
 * <dd>创建时间：  15:55 2020/10/9</dd>
 * <dd>创建人： yuzhiguo</dd>
 * <dt>版本历史: </dt>
 * Date         Author         Version     Description
 * ------------------------------------------------------------------
 * 2020/10/9     yuzhiguo         1.0        1.0 Version
 */
@RestController
@RequestMapping("/menu")
@Slf4j
public class MenuController {

    @Reference
    private MenuService menuService;

    /**
     * 分页查询
     */
    @GetMapping("findPage")
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    public Result findPage(QueryPageBean queryPageBean) {
        log.info("[菜单-分页查询]data:{}", queryPageBean);
        PageResult pageResult =menuService.pageQuery(queryPageBean);
        return new Result(true,MessageConst.ACTION_SUCCESS, pageResult);
    }
    /**
     * 新增
     */
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('MENU_ADD')")
    public Result add(@RequestBody Menu menu){
        log.info("[菜单-新增]data:{}",menu);
        menuService.add(menu);
        return new Result(true,MessageConst.ADD_MENU_SUCCESS);
    }
    /**
     * 编辑
     */
    @RequestMapping("/edit")
    @PreAuthorize("hasAuthority('MENU_EDIT')")
    public Result edit(@RequestBody Menu menu) {
        log.info("[菜单-编辑]data:", menu);
        menuService.edit(menu);
        return new Result(true, MessageConst.EDIT_MENU_SUCCESS);
    }

    /**
     * 根据id删除
     */
    @RequestMapping("/delete")
    @PreAuthorize("hasAuthority('MENU_DELETE')")
    public Result delete(Integer id){
        log.info("[菜单-根据id删除]id：{}",id);
        menuService.delete(id);
        return new Result(true,MessageConst.DELETE_MENU_SUCCESS);
    }
    /**
     * 根据Id查询
     */
    @RequestMapping("/findById")
    @PreAuthorize("hasAuthority('MENU_QUERY')")
    public Result findById(Integer id) {
        log.info("[菜单-根据ID查询]id:{}", id);
        Menu menu= menuService.findById(id);
        return new Result(true,MessageConst.ACTION_SUCCESS,menu);
    }

/*
     * 查询所有菜单
     * @return
     */
    @RequestMapping("/findAll")
    public Result findAll(){
        log.info("菜单查询。。。。。。");
        List<Menu> menuList = menuService.findAll();
        return new Result(true, "查询权限成功",menuList);
    }

    /**
     * 动态权限管理
     * 业务分析-->用户登录-->发送请求-->服务器接收请求-->用户权限判断-->根据用户权限生成相应的菜单数据
     * -->返回数据-->渲染给浏览器
     */
    @RequestMapping("/getMenu")
    public Result getMenu() {
        //获取用户名
        // SecurityContextHolder工具获取到该安全上下文getContext得到上下文getAuthentication获取认证
        //
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        //通过用户名获取查看相应的菜单
        List<Menu> list = menuService.getMenuByUsername(username);
        return new Result(true, "查询菜单成功", list);
    }
}
